package org.duang.render;

import java.io.IOException;
import java.io.PrintWriter;

import org.duang.common.exceptios.ServiceException;
import org.duang.config.Config;



/**
 * XmlRender.
 */
public class XmlRender extends Render {
	
	private static final long serialVersionUID = 4775148244778489992L;
	private static final String defaultContentType = "text/xml;charset=" + Config.getEncoding();
	private String text;
	
	public XmlRender(String text) {
		this.text = text;
	}
	
	private String contentType;
	public XmlRender(String text, String contentType) {
		this.text = text;
		this.contentType = contentType;
	}
	
	public void render() {
		PrintWriter writer = null;
		try {
			response.setHeader("Pragma", "no-cache");	// HTTP/1.0 caches might not implement Cache-Control and might only implement Pragma: no-cache
	        response.setHeader("Cache-Control", "no-cache");
	        response.setDateHeader("Expires", 0);
	        
	        if (contentType == null) {
	        	response.setContentType(defaultContentType);
	        }
	        else {
	        	response.setContentType(contentType);
				response.setCharacterEncoding(Config.getEncoding());
	        }
	        
	        writer = response.getWriter();
	        writer.write(text);
	        writer.flush();	
		} catch (IOException e) {
			throw new ServiceException(e);
		}
		finally {
			if (writer != null)
				writer.close();
		}
	}
}




